package breeze.tool.life.fragments;

import android.preference.PreferenceFragment;
import android.os.Bundle;
import breeze.tool.life.R;
import android.preference.Preference;
import android.view.View.OnClickListener;
import breeze.tool.life.preferences.AppPreference;
import brz.breeze.app_utils.BToast;
import android.preference.EditTextPreference;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import breeze.tool.life.App;
import breeze.tool.life.utils.AppWeatherUtils;

public class AppSettingsFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener,Preference.OnPreferenceChangeListener {

	@Override
	public boolean onPreferenceChange(Preference p1, Object p2) {
		String id = p1.getKey();
		if(id.equals("city_id")){
			mCityID.setSummary(mCityID.getEditText().getText());
			sets.edit().putString("city_id",mCityID.getEditText().getText().toString()).apply();
			AppWeatherUtils.updateWidget(App.getContext());
		}else if("city_name".equals(id)){
			mCityName.setSummary(mCityName.getEditText().getText());
			sets.edit().putString("city_name",mCityName.getEditText().getText().toString()).apply();
			AppWeatherUtils.updateWidget(App.getContext());
		}
		return false;
	}
	

	@Override
	public boolean onPreferenceClick(Preference p1) {
		String id = p1.getKey();
		if(id.equals("output_data")||id.equals("input_data")){
			toast("开发中.....");
		}
		return false;
	}
	
	private SharedPreferences sets;
    
    /*
	*@author BREEZE
	*@date 2021-11-28 08:51:07
    */
    public static final String TAG = "AppSettingsFragment";
	
	private AppPreference mOutputData,mInputData;
	
	private EditTextPreference mCityID,mCityName;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.app_settings);
		init();
		initData();
	}

	private void initData() {
		mCityID.setSummary(sets.getString("city_id","101270101"));
		mCityName.setSummary(sets.getString("city_name","成都"));
	}

	private void init() {
		sets = PreferenceManager.getDefaultSharedPreferences(App.getContext());
		mOutputData = (AppPreference) findPreference("output_data");
		mOutputData.setOnPreferenceClickListener(this);
		mInputData = (AppPreference) findPreference("input_data");
		mInputData.setOnPreferenceClickListener(this);
		mCityID = (EditTextPreference) findPreference("city_id");
		mCityID.setOnPreferenceChangeListener(this);
		mCityName = (EditTextPreference) findPreference("city_name");
		mCityName.setOnPreferenceChangeListener(this);
	}
	
	private void toast(String message){
		BToast.makeText(getActivity(),message,BToast.LENGTH_SHORT).show();
	}
    
}
